package com.jhws.business.property.controller;

import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.owner.bo.OwnerConstant;
import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.business.owner.service.ITblOwnerDimService;
import com.jhws.business.property.bo.PropertyConstant;
import com.jhws.business.property.bo.PropertyStaff;
import com.jhws.business.property.bo.PropertyStaffOrg;
import com.jhws.business.property.service.PropertyService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.common.base.bo.Global;
import com.jhws.sys.appUser.service.ITblSipAcountDimService;
import com.jhws.util.GetId;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/property/staff")
public class PropertyStaffController {

    @Resource
    PropertyService IPropertyService;
    @Resource
    ITblOwnerDimService IOwnerService;
    @Resource
    ITblUserDimService IUserService;
    @Resource
    ITblSipAcountDimService ISipAccountService;
    @Resource
    ITblCommDimService ICommService;

    @RequestMapping("getByComm.x")
    @ResponseBody
    public JsonRespData getByComm(String commCode) {
        if (StringUtils.isNotEmpty(commCode))
            return new JsonRespData("", 1, IPropertyService.getOrgStaffsByCommCode(commCode));

        return new JsonRespData("", 1, IPropertyService.getOrgStaffsByUser(IUserService.currentUser().getLogName()));
    }

    @RequestMapping("deleteOrgStaff.x")
    @ResponseBody
    public JsonRespData deleteOrgStaff(Long staffOrgId, Long staffId) throws Exception {
        IPropertyService.deleteStaffOfOrg(staffOrgId, staffId);
        return new JsonRespData("", 1, "");
    }

    @RequestMapping("save.x")
    @ResponseBody
    public JsonRespData save(PropertyStaff staff, Long staffId, PropertyStaffOrg staffOrg, Long staffOrgId, String devBinding) throws Exception {
        staff.setOwner(IUserService.currentUser().getLogName());
        String bingdingDev = staff.getCommCode() + "0000000" + (StringUtils.isEmpty(devBinding) ? '0' : devBinding);
        TblOwnerDimBo owner = null;
        if (staffId != null) {
            PropertyStaff s = IPropertyService.getStaff(staffId);
            String ownerCode = s.getOwnerCode();
            if (StringUtils.isNotEmpty(ownerCode))
                owner = IOwnerService.findTblOwnerDimById(ownerCode);
        }else{
            List<TblOwnerDimBo> owners = IOwnerService.findTblOwnerDimByWhere("where owner_mob = '"+staff.getPhoneNum()+"'");
            for(TblOwnerDimBo o : owners){
                if(!o.getOwnerName().equals(staff.getStaffName())){
                    String errStr = "";
                    if(o.getApplyType() == OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER)
                        errStr = "该手机号存在管理员 ["+o.getCommName()+"]"+o.getOwnerName();
                    else if(o.getApplyType() == OwnerConstant.OWNER_TYPE_APPLYING)
                        errStr = "该手机号存在业主(申请中)"+o.getRoomInfo()+" "+o.getOwnerName();
                    else
                        errStr = "该手机号存在业主"+o.getRoomInfo()+" "+o.getOwnerName();
                    return new JsonRespData("",-1,errStr);
                }
            }
        }

        /* 开APP权限 */
        if (staff.getRole() != null) {
            if (owner == null) {
                owner = new TblOwnerDimBo();
                owner.setOwnerCode(GetId.randomID());
                owner.setLastModify(DateUtil.getTimeNowFmtSec());
                owner.setOwnerMob(staff.getPhoneNum());
                owner.setCommCode(staff.getCommCode());
                owner.setApplyType(OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER);
                owner.setOwnerName(staff.getStaffName());
                staff.setOwnerCode(owner.getOwnerCode());
                TblCommDimBo comm = ICommService.findTblCommDimById(staff.getCommCode());
                if (comm != null)
                    owner.setCommName(comm.getCommName());
            } else {
                owner.setLastModify(DateUtil.getTimeNowFmtSec());
                owner.setOwnerMob(staff.getPhoneNum());
                owner.setCommCode(staff.getCommCode());
                owner.setApplyType(OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER);
                owner.setOwnerName(staff.getStaffName());
                staff.setOwnerCode(owner.getOwnerCode());
                TblCommDimBo comm = ICommService.findTblCommDimById(staff.getCommCode());
                if (comm != null)
                    owner.setCommName(comm.getCommName());
                if (!bingdingDev.equals(owner.getRoomInfoNum())) {
                    ISipAccountService.removeSipBinding(owner.getOwnerMob(), owner.getRoomInfoNum());
                }
            }

            /* 添加管理机绑定 */
            if (StringUtils.isNotEmpty(devBinding)) {
                ISipAccountService.addSipBinding(staff.getPhoneNum(), bingdingDev, Global.BINDING_TYPE_MANAGER, 0);
                staff.setDevBinding(bingdingDev);
            }else
                staff.setDevBinding("");

            owner.setRoomInfoNum(bingdingDev);
            IOwnerService.save(owner);

        }
        /* 关APP权限 */
        else if (staff.getRole() == null && owner != null) {
            ISipAccountService.removeSipBinding(owner.getOwnerMob(), owner.getRoomInfoNum());
            IOwnerService.removeDim(owner.getOwnerCode());
            staff.setDevBinding("");
        }

        staff.setId(staffId);
        IPropertyService.save(staff);
        staffOrg.setStaffId(staff.getId());
        staffOrg.setId(staffOrgId);
        IPropertyService.save(staffOrg);
        return new JsonRespData("", 1, "");
    }

    @RequestMapping("getStaffInfo.x")
    @ResponseBody
    public JsonRespData getStaffInfo(Long id, String ownerCode, String devBinding) {
        Map retMap = new HashMap();
        if (id != null) {
            PropertyStaff staff = IPropertyService.getStaff(id);
            retMap.put("staff", staff);

            if (StringUtils.isNotEmpty(devBinding) && StringUtils.isNotEmpty(staff.getOwnerCode())) {
                TblOwnerDimBo owner = IOwnerService.findTblOwnerDimById(staff.getOwnerCode());
                retMap.put("devBinding", owner.getRoomInfoNum().substring(11, 12));
            }
        }

        if (StringUtils.isNotEmpty(ownerCode)) {
            List<PropertyStaff> list = IPropertyService.findStaffByWhere("owner_code = '" + ownerCode + "'");
            if (list != null && list.size() != 0)
                retMap.put("staff", list.get(0));
        }

        return new JsonRespData("", 1, retMap);
    }

    @RequestMapping("getByOrg.x")
    @ResponseBody
    public JsonRespData getByOrg(Long orgId, Integer staffType) {
        return new JsonRespData("", 1, IPropertyService.getStaffByOrg(orgId, staffType));
    }
    
    /**
     * 获取某个组织的某种类型的员工列表
     *
     * @param orgIds    组织id列表
     * @param staffType 员工类型，也就是app物业端子角色
     * @return
     */
    @RequestMapping("getByOrgs.x")
    @ResponseBody
    public JsonRespData getByOrgs(String orgIds, Integer staffType) {
        return new JsonRespData("", 1, IPropertyService.getStaffByOrgs(orgIds, staffType));
    }


    @RequestMapping("getStaffByAppPerm.x")
    @ResponseBody
    public JsonRespData getStaffByAppPerm(String commCode, String module) {
        Long start = System.currentTimeMillis();
        List list = IPropertyService.getStaffByCommAppPerm(commCode, module);
        Long end = System.currentTimeMillis() - start;
        return new JsonRespData(end + "毫秒", 1, list);
    }

    @RequestMapping("debugMoveOldPropertyAppInfo.x")
    @ResponseBody
    public JsonRespData debugMoveOldPropertyAppInfo() throws Exception {
        int count = 0;
        List<TblOwnerDimBo> ownerList = IOwnerService.findTblOwnerDimByWhere("where apply_type = '" + OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER + "'");
        for (TblOwnerDimBo owner : ownerList) {
            List<PropertyStaff> staffList = IPropertyService.findStaffByWhere("owner_code = '" + owner.getOwnerCode() + "'");
            if (staffList == null || staffList.size() == 0) {
                PropertyStaff staff = new PropertyStaff();
                staff.setStaffType(PropertyConstant.POSITION_STAFF);
                staff.setOwnerCode(owner.getOwnerCode());
                staff.setCommCode(owner.getCommCode());
                staff.setPhoneNum(owner.getOwnerMob());
                if (StringUtils.isNotEmpty(owner.getOwnerName()))
                    staff.setStaffName(owner.getOwnerName());
                else
                    staff.setStaffName("匿名");
                IPropertyService.save(staff);
                PropertyStaffOrg staffOrg = new PropertyStaffOrg();
                staffOrg.setStaffId(staff.getId());
                staffOrg.setOrgId(0L);
                IPropertyService.save(staffOrg);
                count++;
            }
        }

        return new JsonRespData("", 1, "修改了" + count + "条数据");
    }

}
